from django.contrib import admin
from .models import (
    AIServiceConfig, EmailConfig, DingTalkConfig,
    PushKeyword, PushTask, PushHistory
)

@admin.register(AIServiceConfig)
class AIServiceConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'url', 'created_at', 'updated_at')
    search_fields = ('name', 'url')
    list_filter = ('created_at', 'updated_at')

@admin.register(EmailConfig)
class EmailConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'email_address', 'smtp_server', 'created_at', 'updated_at')
    search_fields = ('name', 'email_address', 'smtp_server')
    list_filter = ('created_at', 'updated_at')
    fieldsets = (
        (None, {'fields': ('name', 'email_address')}),
        ('SMTP配置', {'fields': ('smtp_server', 'smtp_port', 'smtp_username', 'smtp_password', 'use_ssl')}),
    )

@admin.register(DingTalkConfig)
class DingTalkConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'webhook_url', 'created_at', 'updated_at')
    search_fields = ('name', 'webhook_url')
    list_filter = ('created_at', 'updated_at')

@admin.register(PushKeyword)
class PushKeywordAdmin(admin.ModelAdmin):
    list_display = ('keyword', 'created_at', 'updated_at')
    search_fields = ('keyword',)
    list_filter = ('created_at', 'updated_at')

@admin.register(PushTask)
class PushTaskAdmin(admin.ModelAdmin):
    list_display = ('keyword', 'task_type', 'schedule_time', 'is_active', 'created_at', 'updated_at')
    search_fields = ('keyword__keyword',)
    list_filter = ('task_type', 'is_active', 'created_at', 'updated_at')
    raw_id_fields = ('keyword', 'email_config', 'dingtalk_config')

@admin.register(PushHistory)
class PushHistoryAdmin(admin.ModelAdmin):
    list_display = ('task', 'status', 'executed_at')
    search_fields = ('task__keyword__keyword', 'status')
    list_filter = ('status', 'executed_at')
    raw_id_fields = ('task',)
